package collections;

/**
 * @author Gouke Leplat
 */


import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import Exceptions.ConfigurationException;
import be.groept.ace.cursusplanning.controller.AppConfig;
import be.groept.ace.cursusplanning.model.Cursist;
import be.groept.ace.cursusplanning.model.CursusHistoriek;
import be.groept.ace.cursusplanning.persistency.DBMS;
import be.groept.ace.cursusplanning.persistency.DBMS_Tekstbestanden;


public class Opgave4Bis2 {

	class CursistCompareToByCursistNummer implements Comparator<Cursist>{

		@Override
		public int compare(Cursist o1, Cursist o2) {
			return o1.getCursistNummer().compareTo(o2.getCursistNummer());
		}
	}
	
	@SuppressWarnings("unchecked")
	public static void main(String[] args) throws ConfigurationException{
		Opgave4Bis2 bis2 = new Opgave4Bis2();
		
		Iterator<?> it = bis2.getCursisten().entrySet().iterator();
		Map.Entry<String,Cursist> entry ;
		
		while (it.hasNext()){
			entry = (Map.Entry<String, Cursist>) it.next();
			System.out.println("De historieken voor cursist "+ entry.getValue().getCursistNaam() + " (nr: " + entry.getValue().getCursistNummer() + "):");
			
			bis2.printHistorieken(entry.getValue().getCursistNummer());
		}
		
	}
	
	// om te ovedrijven
	// Map<String,Map<Integer,CursusHistoriek>> map = new TreeMap<String,Map<Integer,CursusHistoriek>>();
		
	TreeMap<String,Cursist> cursisten;
	DBMS db;
	
	public Opgave4Bis2() throws ConfigurationException{

		try {
			db = new DBMS(new DBMS_Tekstbestanden(new AppConfig("C:\\_temp\\javaprac\\AppConfig.txt")));
		} catch (ConfigurationException e) {
			throw e;
		}
		fillCursisten();
	}
	
	
	private void fillCursisten(){
		List<Cursist> cten = db.getCursisten().getAll();
		
		cursisten = new TreeMap<String, Cursist>();  
		
		for (int i = 0; i < cten.size(); i++){
			cursisten.put(cten.get(i).getCursistNummer(),cten.get(i));
		}
		
	}
	
	public TreeMap<String, Cursist> getCursisten(){
		return cursisten;
	}
	
	public Cursist getCursist (String cursistnummer){
		
		
		return cursisten.get(cursistnummer);
	}
	
	public void printHistorieken(Cursist cursist){
		
		List<CursusHistoriek> Hken = cursist.getCursushistorieken();
		
		for (int i = 0; i < Hken.size();i++){
			System.out.println(Hken.get(i).getSchooljaar() + " - " + Hken.get(i).getCursus().toString() + " - " + Hken.get(i).getScore());
		}
		
	}
	
	public void printHistorieken(String cursistNummer){
		
		Cursist cursist = getCursist(cursistNummer);
		List<CursusHistoriek> Hken = cursist.getCursushistorieken();
		
		for (int i = 0; i < Hken.size();i++){
			if (Hken.get(i).getScore() == -1){
				System.out.println(Hken.get(i).getSchooljaar() + " - " + Hken.get(i).getCursus().toString() + " - " + "nog niet voltooid");
			}
			else{
				System.out.println(Hken.get(i).getSchooljaar() + " - " + Hken.get(i).getCursus().toString() + " - " + Hken.get(i).getScore());
			}
			
		}
		
	}
	
}
